package com.tomato.security.context;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolderStrategy;
import org.springframework.security.core.context.SecurityContextImpl;

/**
 * Security Context 持有者策略
 *
 * @author lizhifu
 * @since 2022/12/16
 */
public class ThreadLocalSecurityContextHolderStrategy implements SecurityContextHolderStrategy {

	private static final ThreadLocal<SecurityContext> CONTEXT_HOLDER = new ThreadLocal<>();

	@Override
	public void clearContext() {
		CONTEXT_HOLDER.remove();
	}

	@Override
	public SecurityContext getContext() {
		SecurityContext ctx = CONTEXT_HOLDER.get();
		if (ctx == null) {
			ctx = createEmptyContext();
			CONTEXT_HOLDER.set(ctx);
		}
		return ctx;
	}

	@Override
	public void setContext(SecurityContext context) {
		CONTEXT_HOLDER.set(context);
	}

	@Override
	public SecurityContext createEmptyContext() {
		return new SecurityContextImpl();
	}

}
